<template>
  <div class="app-container">
    <el-row :gutter="30">
      <el-col :span="24">
        <div style="margin-top:10px">
          <el-button icon="el-icon-plus" type="primary" size="mini" @click="addData" v-if="add">新增</el-button>
          <el-button icon="el-icon-close" type="danger" size="mini" @click="deleteAny" v-if="batchDel">删除</el-button>
        </div>
        <div class="query-form">
          <el-form ref="tableSearchParam" :model="tableSearchParam" label-width="50px" :inline="true">
            <el-form-item label="名称" label-width="50px">
              <el-input v-model="tableSearchParam.name" style="width:200px" size="small" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="listTableData" icon="el-icon-search" size="small" >查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <br/>
        <el-table
          :data="tableData"
          border
          empty-text="暂无数据"
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="35">
          </el-table-column>
          <el-table-column
            label="行业名称"
            property="name"
            width="140">
          </el-table-column>
          <el-table-column
            label="行业描述"
            property="description"
          >
          </el-table-column>
          <el-table-column
            label="创建人"
            property="createdBy"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="创建时间"
            property="createdTime"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="修改人"
            property="updatedBy"
            width="120"
          >

          </el-table-column>
          <el-table-column
            property="updatedTime"
            label="修改时间"
            width="120">
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="success"
                v-if="update"
                @click="editData(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-close"
                v-if="del"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=this.currentPage
            :page-sizes="[10, 20, 30, 40]"
            :page-size=this.pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total=this.total>
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="actionType=='add'?'新增行业':'修改行业'" :visible.sync="dialogFormVisible" top="10vh">
      <el-form :model="formData" style="width:400px" :rules="rules" ref="ruleForm">
        <el-form-item label="行业名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="formData.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="行业描述" :label-width="formLabelWidth" prop="description">
          <el-input v-model="formData.description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd" v-if="actionType=='add'" :loading="loading">保存</el-button>
        <el-button type="primary" @click="handleEdit" v-else :loading="loading">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .page {
    text-align: center;
    margin-top: 20px;
  }

  .query-form {
    border-bottom: 1px solid #ebeef5;
    padding-top: 25px;
  }

  .user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 10px;
  }
</style>

<script>
  import industryApi from '@/api/industryApi'
  import ImageCropper from '@/components/ImageCropper'

  export default {
    components: {ImageCropper},
    created() {
      this.add = this.permissionValid.validAction(this.permissionValid.permissionRule.industry.add)
      this.update = this.permissionValid.validAction(this.permissionValid.permissionRule.industry.update)
      this.del = this.permissionValid.validAction(this.permissionValid.permissionRule.industry.del)
      this.load = this.permissionValid.validAction(this.permissionValid.permissionRule.industry.load)
      this.batchDel = this.permissionValid.validAction(this.permissionValid.permissionRule.industry.batchDel)
      this.list = this.permissionValid.validAction(this.permissionValid.permissionRule.industry.list)
      this.initData()
    },
    data() {
      return {
        baseUrl: process.env.IMG_BASE_URL,
        add: false,
        update: false,
        del: false,
        load: false,
        batchDel: false,
        list:false,
        dialogsFormVisible: false,
        formsData: {
          templateId: ''
        },
        allTemplateData: [],
        uploadUrl: process.env.BASE_API + '/admin/online/service/company/upload',
        imagecropperShow: false,
        imagecropperKey: 0,
        tableData: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableSearchParam: {
          name: ''
        },
        dialogFormVisible: false,
        formLabelWidth: '110px',
        formData: {
          name: '',
          address: ''

        },
        loading: false,
        actionType: '',
        multipleSelection: [],
        rules: {
          name: [
            {required: true, message: '请输入平台名称', trigger: 'blur'},
            {max: 50, message: '长度不超过50 个字符', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      initData() {
        this.listTableData()
      },
      listTableData() {
        industryApi.listIndustry(this.tableSearchParam, this.currentPage, this.pageSize, (res) => {
          this.tableData = res.list
          this.total = res.total
        }, () => {
        })
      },
      addData() {
        //清空历史值
        this.formData = {
          name: '',
          description: ''
        }
        this.actionType = 'add'
        this.dialogFormVisible = true
        this.$refs['ruleForm'].resetFields()
      },
      handleAdd() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            industryApi.addIndustry(this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })
      },
      editData(index, row) {
        //清空历史值
        this.formData = {
          code: '',
          name: '',
          description: '',
        }
        let that = this
        that.actionType = 'edit'
        this.loading = true
        industryApi.getIndustry(row.id, (res1) => {
          that.formData = res1
          that.formData.id = row.id
          this.loading = false
          that.dialogFormVisible = true
        }, () => {

        })
      },
      handleEdit() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            industryApi.updateIndustry(this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })
      },
      handleDelete(index, row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          industryApi.removeIndustry(row.id, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.listTableData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.listTableData()
      },
      handleSelectionChange(val) {
        this.multipleSelection = []
        for (var ms of val) {
          this.multipleSelection.push(ms.id)
        }
      },
      deleteAny() {
        if (this.multipleSelection.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择记录!'
          })
          return;
        }
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          industryApi.batchRemoveIndustry(this.multipleSelection, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>
